package com.itboye.mengwu.app.ui.mymeng;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.itboye.inageview.ImagePagerActivity;
import com.itboye.inageview.NoScrollGridAdapter;
import com.itboye.inageview.NoScrollGridView;
import com.itboye.mengwu.R;
import com.itboye.mengwu.luntan.comment.adapter.CommentReplyAdapter;
import com.itboye.mengwu.luntan.comment.adapter.CommentAdapter.ViewHolder;
import com.itboye.mengwu.luntan.comment.bean.Comment;
import com.itboye.mengwu.luntan.comment.view.MyListView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class NewPingLunAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private ViewHolder viewHolder;
	private Context context;
	private OnClickListener replyToCommentListener;
	private OnClickListener replyToReplyListener;
	private List<Comment> commentList;

	public NewPingLunAdapter(Context context, List<Comment> commentList,
			OnClickListener replyToCommentListener,
			NewPingHUiTie commentReplyAdapter,
			OnClickListener replyToReplyListener) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.commentList = new ArrayList<Comment>();
		this.commentList.addAll(commentList);
		this.replyToCommentListener = replyToCommentListener;
		this.replyToReplyListener = replyToReplyListener;
	}

	public void clearList() {
		this.commentList.clear();
	}

	public void updateList(List<Comment> commentList) {
		this.commentList.addAll(commentList);
	}

	@Override
	public int getCount() {
		return commentList.size();
	}

	@Override
	public Comment getItem(int position) {
		return commentList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_pinglunme, null);
			viewHolder = new ViewHolder();
			// viewHolder.iv_user_photo = (ImageView) convertView
			// .findViewById(R.id.iv_user_photo);
			viewHolder.txt_pingluntitle = (TextView) convertView
					.findViewById(R.id.txt_pingluntitle);
			viewHolder.txtPingLuncontent = (TextView) convertView
					.findViewById(R.id.txtPingLuncontent);
			viewHolder.iv_avatar = (ImageView) convertView
					.findViewById(R.id.iv_avatar);
			viewHolder.textHui = (TextView) convertView
					.findViewById(R.id.textHui);
			viewHolder.myListView = (MyListView) convertView
					.findViewById(R.id.myListView);
			// viewHolder.gridview = (NoScrollGridView)
			// convertView.findViewById(R.id.gridview);
			viewHolder.textHui.setTag(position);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		Comment comment = getItem(position);
		viewHolder.txt_pingluntitle.setText(comment.getUsername());
		viewHolder.txtPingLuncontent.setText(comment.getContent());
		// 设置评论列表的点击效果透明
		viewHolder.myListView.setSelector(new ColorDrawable(
				Color.TRANSPARENT));
		// 判断当前评论是否有回复
		if (commentList.get(position).getReplyList() != null
				&& commentList.get(position).getReplyList().size() != 0) {
//			viewHolder.myListView
//					.setAdapter(new CommentReplyAdapter(context, commentList
//							.get(position).getReplyList(), position,
//							replyToReplyListener));
		}
		// 记录点击回复按钮时对应的position,用于确定所回复的对象
		viewHolder.textHui.setTag(position);
		viewHolder.textHui.setOnClickListener(replyToCommentListener);
		return convertView;

	}

	public class ViewHolder {
		private ImageView iv_avatar;
		private ImageView iv_user_photo; // 评论者 头像
		private TextView txt_pingluntitle; // 评论者 昵称
		private TextView txtPingLuncontent; // 评论者 一级品论内容
		// private TextView tv_user_comment_date; //
		private TextView textHui; // 评论者 二级评论按钮
		private MyListView myListView; // 评论者 二级品论内容列表
		// private NoScrollGridView gridview;
	}

}
